package com.xb.loan.usercenter.user.user.service.impl;

import com.xb.loan.log.exception.BizException;
import com.xb.loan.usercenter.service.user.IUserFeedbackService;
import com.xb.loan.usercenter.service.user.IUserMsgService;
import com.xb.loan.usercenter.service.user.bean.UserFeedbackReplyVO;
import com.xb.loan.usercenter.service.user.bean.UserFeedbackVO;
import com.xb.loan.usercenter.service.user.bean.UserMsgVO;
import com.xb.loan.usercenter.user.user.dao.UserFeedbackDao;
import com.xb.loan.usercenter.user.user.dao.bean.UserFeedback;
import com.xb.loan.util.cache.redis.comm.FormatOperation;
import com.xb.loan.util.common.ObjectOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service("userFeedbackService")
public class UserFeedbackServiceImpl implements IUserFeedbackService {

	@Autowired
    UserFeedbackDao userFeedbackDao;
	
	@Autowired
    IUserMsgService userMsgService;
	
	@Override
	public int add(UserFeedbackVO userFeedbackVO) throws BizException {
		
		UserFeedback userFeedback = ObjectOperation.convertByClasses(userFeedbackVO, UserFeedback.class);
		String id = FormatOperation.assemblySeq("common", 4, FormatOperation.formatDateToYMDHms(new Date()), "");
		userFeedback.setId(id);
		return userFeedbackDao.insertSelective(userFeedback);
	}

	@Override
	public int update(UserFeedbackVO userFeedbackVO) throws BizException{

		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", userFeedbackVO.getId());
		params.put("status", userFeedbackVO.getStatus());
		return userFeedbackDao.update(params);
	}

	@Override
	public int delete(String id) throws BizException{
		
		return userFeedbackDao.delete(id);
	}

	@Override
	public List<UserFeedbackVO> queryUserFeedback(String partnerId, String userId, String phone,
                                                  Integer status, String title, String content, String startTime, String endTime, int pageNum, int numPerPage) throws BizException{

		Map<String, Object> params = new HashMap<String, Object>();
		params.put("partnerId", partnerId);
		params.put("userId", userId);
		params.put("phone", phone);
		params.put("status", status);
		params.put("title", title);
		params.put("content", content);
		params.put("startTime", startTime);
		params.put("endTime", endTime);
		params.put("pageNum", pageNum);
		params.put("numPerPage", numPerPage);
		List<UserFeedback> ufList = userFeedbackDao.queryUserFeedback(params);
		return ObjectOperation.convertByClassesList(ufList, UserFeedbackVO.class);
	}

	@Override
	public int queryUserFeedbackCount(String partnerId,
                                      String userId, String phone, Integer status, String title, String content,
                                      String startTime, String endTime) throws BizException{
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("partnerId", partnerId);
		params.put("userId", userId);
		params.put("phone", phone);
		params.put("status", status);
		params.put("title", title);
		params.put("content", content);
		params.put("startTime", startTime);
		params.put("endTime", endTime);
		return userFeedbackDao.queryUserFeedbackCount(params);
	}

	@Override
	public List<UserFeedbackReplyVO> queryUserFeedbackReply(String partnerId,
                                                            String userId, String phone, Integer status, String title,
                                                            String content, String startTime, String endTime, int pageNum,
                                                            int numPerPage) throws BizException{
		
		//查询意见反馈
		List<UserFeedbackVO> feedbackVOList = queryUserFeedback(partnerId, userId, phone, status, title, content,
				startTime, endTime, pageNum, numPerPage);
		
		if(feedbackVOList == null || feedbackVOList.size() == 0){
			return null;
		}
		List<String> sysIdList = new ArrayList<String>();
		Map<String,UserFeedbackReplyVO> map = new HashMap<String,UserFeedbackReplyVO>();
		List<UserFeedbackReplyVO> userFeedbackReplyVOList = new ArrayList<UserFeedbackReplyVO>();
		
		for(UserFeedbackVO feedbackVO : feedbackVOList){
			//当前用户的意见反馈id集合
			sysIdList.add(feedbackVO.getId());
			
			UserFeedbackReplyVO userFeedbackReplyVO= new UserFeedbackReplyVO();
			userFeedbackReplyVO.setUserFeedbackVO(feedbackVO);
			userFeedbackReplyVOList.add(userFeedbackReplyVO);
			map.put(feedbackVO.getId(), userFeedbackReplyVO);
		}

		//查询意见反馈的回复
		List<UserMsgVO> msgVOList = userMsgService.queryUserMsg(sysIdList,userId);
		for(UserMsgVO msgVO : msgVOList){
			//将回复设置到对应的意见反馈map里面去
			UserFeedbackReplyVO userFeedbackReplyVO = map.get(msgVO.getSysId());
			//针对有多条回复的通用编码
			List<UserMsgVO> rtnMsgVOList = userFeedbackReplyVO.getUserMsgVOList();
			if(rtnMsgVOList == null || rtnMsgVOList.size() == 0){
				rtnMsgVOList = new ArrayList<UserMsgVO>();
			}
			rtnMsgVOList.add(msgVO);
			userFeedbackReplyVO.setUserMsgVOList(rtnMsgVOList);
		}
		
		//返回用户需要的VO集合
		if(userFeedbackReplyVOList == null || userFeedbackReplyVOList.size() == 0){
			userFeedbackReplyVOList = null;
		}
		return userFeedbackReplyVOList;
	}

}
